// 这是一个用于控制页面中视频播放的文件
// 将template嵌入页面并对page调用injection把所有的handler注入页面即可
// 会占用一部分属性名，注意不要重名了

export default function injection(page) {
  Object.assign(page, {
    videoPlayerContext: null,
    playVideo(e) {
      let src = e.currentTarget.dataset.src
      console.log(src,'src')
      if (src)
        this.startVideoPlayer(src)
      else
        wx.showToast({
          title: '没有找到视频路径',
          icon: 'none',
        })
    },
    startVideoPlayer(src) {
      // TODO: 下面这一行是调试语句需要删除
      // src = src.replace('http://192.168.1.126:8080/uploads/ufa', 'http://192.168.1.11:8099/uploads/ufa').replace('http://192.168.1.126:8080/uploads/ufa', '')
      console.log(src,'srcsrcsrcsrcsrc')
      this.setData({
        videoSrc: src,
      }, () => {
        wx.showLoading({ title: '加载视频中' })
        // this.videoPlayerContext.play()
      })
    },
    onVideoPlay() {
      console.log('开始播放')
      wx.hideLoading()
      
      // this.videoPlayerContext.requestFullScreen()
    },
    onVideoEnded() {
      console.log('结束播放')
      wx.hideLoading()
      // this.videoPlayerContext.exitFullScreen()
    },
    onVideoFullscreenchange(e) {
      if (!e.detail.fullScreen) {
        this.videoPlayerContext.stop()
      }
      wx.hideTabBar({ aniamtion: false })
    },
    onVideoError(e) {
      wx.hideLoading()
      wx.showToast({
        title: `播放失败:${e.detail.errMsg}`,
        icon: 'none'
      })
    }
  })

  let ready = page.onReady
  page.onReady = function (res) {
    ready && ready.call(this)
    this.videoPlayerContext = wx.createVideoContext('myVideo')
  }
  return page
}